package cn.lliiooll.cc;

import org.bukkit.Bukkit;
import org.bukkit.command.Command;
import org.bukkit.command.CommandExecutor;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;

import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.plugin.java.JavaPlugin;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 *  CustomCommand
 *  自定义玩家进服指令或说的话
 *
 *  by: lliiooll
 * @author lliiooll
 * @see org.bukkit.plugin.java.JavaPlugin
 *
 */

public class CustomCommand extends JavaPlugin implements CommandExecutor, Listener, Runnable {


    private String pre = "§c§l[§bCustomCommand§c§l]§f ";
    private boolean debug;
    private Player p;

    @Override
    public void onLoad(){
        info("&b插件加载中...&2作者:&e lliiooll");
    }

    @Override
    public void onEnable(){
        Bukkit.getPluginManager().registerEvents(this,this);
        Bukkit.getPluginCommand("customcommand").setExecutor(this);
        if(!new File(getDataFolder(),"config.yml").exists()){
            saveDefaultConfig();
        }
        debug = getConfig().getBoolean("debug");
        info("&b插件加载完毕!&2作者:&e lliiooll");
    }

    @Override
    public void onDisable(){
        info("&b插件卸载完毕!&2作者:&e lliiooll");
    }

    @Override
    public void run() {
        Player p = this.p;
        List<String> l = (List<String>) getConfig().getList("cc");
        int i = 0;
        debug("有 " + l.size() + " 个数据");
        List<String> l1 = new ArrayList<String>();
        try {
            while (i < l.size()) {
                String m = l.get(i);
                debug("数据 " + m);
                debug("event为 " + getConfig().getString(m + ".event"));
                if (getConfig().getString(m + ".permission").equals("")) {
                    if (use(getConfig().getString(m + ".practitioners"), p, (List<String>) getConfig().getList(m + ".cmd"))) {
                        debug("执行成功");
                        //break;
                    } else {
                        debug("执行失败");
                        //break;
                    }
                } else {
                    if (use(getConfig().getString(m + ".practitioners"), p, getConfig().getString(m + ".permission"), (List<String>) getConfig().getList(m + ".cmd"))) {
                        debug("执行成功");
                        //break;
                    } else {
                        debug("执行失败");
                        //break;
                    }
                }
                i++;
                debug(i + " 次循环");
            }
            debug("循环结束");
        }catch (Exception e){
            error("发生一个错误: " + e.getMessage());
            error("在: " + e.getLocalizedMessage().replaceAll("\n","\n§c在"));
        }
    }

    @EventHandler
    public void onJoin(PlayerJoinEvent e){
        this.p = e.getPlayer();
        Thread t = new Thread(this,"CustomCommand_" + p.getUniqueId().toString());
        t.start();
    }


    @Override
    public boolean onCommand(CommandSender sender, Command command, String label, String[] args) {
        this.reloadConfig();
        sender.sendMessage(pre + "§2配置重载完成");
        return true;
    }

    private void info(String msg){
        Bukkit.getConsoleSender().sendMessage(pre + msg.replace("&","§"));
    }

    private void error(String msg){
        Bukkit.getConsoleSender().sendMessage(pre + msg.replace("&","§"));
    }
    private void debug(String msg){
        if(debug){
            Bukkit.getConsoleSender().sendMessage(pre + " §eDEBUG §b" + msg.replace("&","§"));
        }
    }

    private boolean use(String key,Player p,String permission,List<String> useCmd){
        if(p.hasPermission(permission)){
            debug("类型为" + key);
            switch (key){
                case "player":
                    int i = 0;
                    while(i < useCmd.size()){
                        debug("要执行的指令为" + useCmd.get(i));
                        p.chat(useCmd.get(i));//此处null 或许是useCmd null? 亦或是get到了null??(极大可能)
                        i++;
                    }
                    return true;

                case "op":
                    int i1 = 0;
                    while(i1 < useCmd.size()){
                        debug("要执行的指令为" + useCmd.get(i1));
                        if(p.isOp()){
                            debug("玩家是op");
                            p.chat(useCmd.get(i1));
                        }else{
                            debug("玩家不是op");
                            p.setOp(true);
                            p.chat(useCmd.get(i1));
                            p.setOp(false);
                        }
                        i1++;
                    }
                    return true;
                default:
                    break;

            }
        }
        return false;
    }

    private boolean use(String key,Player p,List<String> useCmd){
        debug("类型为" + key);
        switch (key){
            case "player":
                int i = 0;
                while(i < useCmd.size()){
                    debug("要执行的指令为" + useCmd.get(i));
                    try{
                        p.chat(useCmd.get(i));
                    }catch (Exception e){
                        error("发生一个错误: " + e.getMessage());
                        error("在: " + e.getLocalizedMessage().replaceAll("\n","\n§c在"));
                    }
                    i++;
                }
                return true;
            case "op":
                int i1 = 0;
                while(i1 < useCmd.size()){
                    debug("要执行的指令为" + useCmd.get(i1));
                    if(p.isOp()){
                        debug("玩家是op");
                        p.chat(useCmd.get(i1));
                    }else{
                        debug("玩家不是op");
                        p.setOp(true);
                        p.chat(useCmd.get(i1));
                        p.setOp(false);
                    }
                    i1++;
                }
                return true;
            default:
                break;

        }
        return false;
    }
}
